package com.mall.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: 谢君臣
 * @Date: 2022/7/30-11:31
 * @version: 1.0
 * @function:
 */
@Configuration
public class ESConfig {

    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }


    private String hostname = "192.168.133.100";
    private int Port = 9200;
    private String scheme = "http";

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        RestClientBuilder httpClientBuilder = RestClient.builder(
                new HttpHost(hostname, Port));
        // Create the HLRC
        RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);
        // Create the new Java Client with the same low level client
        ElasticsearchTransport transport = new RestClientTransport(
                hlrc.getLowLevelClient(),
                new JacksonJsonpMapper()
        );
        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        return esClient;
    }


}
